
/** 简单拷贝 */
export const simpleCopy = (obj) => {
  return JSON.parse(JSON.stringify(obj))
}

/** json转formData */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const jsonToFormData = (json) => {
  const formData = new FormData()
  for (const key in json) {
    formData.append(key, json[key])
  }
  return formData
}

/** 模糊手机号 */
export const ofuscateMobile = (mobile) => {
  if (!mobile) {
    return ''
  }
  return mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
}

/** 获取安全对象 */
export const getSafeObj = (json) => {
  if (typeof json === 'undefined' || json === null) {
    return {}
  }
  if (typeof json === 'string') {
    return JSON.parse(json)
  }
  return json
}

/** 浏览器返回 */
export const browserBack = () => {
  // 使用场景: 避免返回首页出发路由守卫触发动画导致页面重绘
  window.history.go(-1)
}

/** 拆分金额 */
export const splitAmount = (amount) => {
  const [int, decimal = '00'] = amount.split('.')
  return {
    int: int || '0',
    decimal: `.${decimal.padEnd(2, '0')}`,
  }
}

/** 防抖 */
export const throttle = (func, delay) => {
  let last = 0
  return function () {
    const now = new Date().getTime()
    if (now - last >= delay) {
      func()
      last = now
    }
  }
}
/** 校验手机号 */
export const validMobile = (v) => {
  return v && /^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(v)
}